package com.xb.loan.log.task;

import com.xb.loan.log.IUserLogService;
import com.xb.loan.log.bean.UserLogVO;
import com.xb.loan.log.constant.LogConst;
import com.xb.loan.util.cache.redis.RedisQueueManager;
import com.xb.loan.util.log.DefaultLoggerFacotry;
import com.xb.loan.util.log.LogFormatUtil;
import com.xb.loan.util.qrtz.task.AbstractCronTask;
import com.xb.loan.util.queue.Queue;
import org.slf4j.Logger;

/**
 * 日志保存任务
 */
public class UserLogTask extends AbstractCronTask {

    private static Logger logger = DefaultLoggerFacotry.getLogger(UserLogTask.class);
   
    private IUserLogService userLogService;
    
    public UserLogTask() {
        super();
    }

    public UserLogTask(String cron) {
        //设置任务名称
        this.setJobName("UserLogTask");
        //设置执行时间策略
        this.setCron_expression(cron);
    }

    @Override
    public void run() {
    	
    	userLogService = getSpringBean("userLogService");
    	
        logger.info(LogFormatUtil.getFormatMsg(new String[]{"module" , "msg"},
                new Object[]{"UserLogTask", " start."}));
        try{
        	UserLogVO userLogVO = getLog();
            
            while (userLogVO != null ){
                
            	userLogService.insertUserLog(userLogVO);
            	logger.info(LogFormatUtil.getFormatMsg(new String[]{"module" , "msg"},
                        new Object[]{"UserLogTask", userLogVO.getUserId()+userLogVO.getLogModule()}));
            	userLogVO = getLog();
            };
            
            logger.info(LogFormatUtil.getFormatMsg(new String[]{"module" , "msg"},
                    new Object[]{"UserLogTask", " end."}));
        }catch(Exception e){
            logger.error(LogFormatUtil.getFormatMsg(new String[]{"module" , "msg"} ,
                    new String[]{"UserLogTask", "run exception."} ), e);
        }
    }
    
    private UserLogVO getLog() {
        Queue<UserLogVO> queue = RedisQueueManager.getQueue(LogConst.RedisQueue_Log, UserLogVO.class);
        return queue.poll();
    }
    
    
}
